<template>
  <div id="service">
    <div class="header">
      <p>服务管理</p>
    </div>
    <div class="middle">
      <el-input
        v-model="input"
        placeholder="请输入项目名称、美疗师、企业名称查询"
      ></el-input>
      <el-button type="primary" @click="query">查询</el-button>
    </div>
    <div class="form">
      <el-table :data="showTableData" stripe style="width: 100%">
        <el-table-column prop="name" label="项目名称"></el-table-column>
        <el-table-column prop="price" label="项目价格"> </el-table-column>
        <el-table-column prop="time" label="项目时长"> </el-table-column>
        <el-table-column
          prop="serviceName"
          label="企业/美疗师"
        ></el-table-column>
        <el-table-column prop="state" label="服务状态"
          ><template slot-scope="scope">
            <div>{{ scope.row.state === 1 ? "已屏蔽" : "正常" }}</div>
          </template></el-table-column
        >
        <el-table-column prop="date" label="创建时间"></el-table-column>
        <el-table-column prop="operation" label="操作">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.state === 1 ? 'success' : 'danger'"
              @click="shielding(scope.row)"
              class="btn"
            >
              {{ scope.row.state === 1 ? "正常" : "屏蔽" }}
            </el-tag>
            <el-tag @click="details(scope.row.id)" class="btn">删除</el-tag>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.length"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "Service",
  data() {
    return {
      input: "",
      currentPage: 1, //放当前页
      pageSize: 5, //每页条数
      tableData: [], //后端拿给我的数据
      multipleSelection: "", //状态改变的
      delete: "", //删除
    };
  },
  methods: {
    //和分页相关的函数
    //每页多少条数据被改变的时候触发的函数
    handleSizeChange(val) {
      this.pageSize = val;
    },
    //当前页被改变的时候触发的函数
    handleCurrentChange(val) {
      this.currentPage = val;
    },

    // 获取所有的数据
    getStu() {
      this.$axios({
        url: "http://172.16.113.23:8080/project/selectAllbig",
        method: "get",
      })
        .then((res) => {
          if (res.code === 200) {
            this.tableData = res.data;
          } else {
            this.$message({
              message: "服务器出错了",
              type: "error",
            });
          }
        })
        .catch((err) => {
          console.log("获取所有的项目信息失败", err);
        });
    },

    // 查询
    query() {
      let projectname = this.input;
      this.$axios({
        url: "http://172.16.113.23:8080/project/selectProject",
        method: "get",
        params: {
          name: projectname,
        },
      })
        .then((res) => {
          if (res.code === 200) {
            this.tableData = res.data;
          } else {
            this.$message({
              message: "服务器出错了",
              type: "error",
            });
          }
        })
        .catch((err) => {
          console.log("获取所有的项目信息失败", err);
        });
    },
    // 删除
    details(id) {
      this.delete = id;
      this.$confirm("您是否确定删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$axios({
          url: "http://172.16.113.23:8080//project/delpro",
          method: "post",
          data: this.delete,
          headers: {
            "Content-Type": "application/json",
          },
        })
          .then((res) => {
            if (res.code === 200) {
              this.getStu();
            } else {
              this.$message({
                message: "服务器出错了",
                type: "error",
              });
            }
          })
          .catch((err) => {
            console.log("获取所有的项目信息失败", err);
          });
      });
    },

    // 屏蔽
    shielding(row) {
      this.multipleSelection = row.id;
      if (row.state === 1) {
        this.$axios({
          url: "http://172.16.113.23:8080/project/updateState0",
          method: "post",
          data: this.multipleSelection,
          headers: {
            "Content-Type": "application/json",
          },
        })
          .then((res) => {
            if (res.code === 200) {
              this.getStu();
            } else {
              this.$message({
                message: "服务器出错了",
                type: "error",
              });
            }
          })
          .catch((err) => {
            console.log("获取所有的项目信息失败", err);
          });
      } else {
        this.$axios({
          url: "http://172.16.113.23:8080/project/updateState1",
          method: "post",
          data: this.multipleSelection,
          headers: {
            "Content-Type": "application/json",
          },
        })
          .then((res) => {
            if (res.code === 200) {
              this.getStu();
            } else {
              this.$message({
                message: "服务器出错了",
                type: "error",
              });
            }
          })
          .catch((err) => {
            console.log("获取所有的项目信息失败", err);
          });
      }
    },

    // 时间戳
  },
  mounted() {},
  computed: {
    showTableData() {
      let start = this.pageSize * (this.currentPage - 1);
      let end = start + this.pageSize;
      let data = this.tableData.slice(start, end);
      return data;
    },
  },
  created() {
    this.getStu();
  },
  watch: {},
};
</script>

<style scoped lang="less">
.header {
  width: 100%;
  height: 30px;
  // background-color: aqua;
  line-height: 30px;
}
.el-input {
  width: 300px;
  margin-right: 30px;
}
.form {
  margin-top: 30px;
  .el-button {
    width: 50px;
    height: 40px;
    padding: 0;
  }
}
.btn {
  cursor: pointer;
  margin-right: 10px;
}
.el-pagination {
  text-align: center;
}
</style>
